package cn.netjava.robo3d.device;

import java.text.DecimalFormat;

import cn.netjava.robo3d.RoboTank;

import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jmex.font2d.Font2D;
import com.jmex.font2d.Text2D;

public class OilrateShow extends Node {

	private int Gear;
	private float Maxoilrate;
	private float oilrate;
	private Text2D text2d;
	private Text2D text2dother;
	private String textother;
	private String bar;
	private RoboTank car;
	public OilrateShow(RoboTank car){
		this.setName("oilrateshowtext2d");
		this.oilrate=car.getoilrate();
		this.car=car;
		Maxoilrate=car.getMaxoilrate();

		bar="";
		createtext();
	}
	public void createtext(){
		double count=oilrate*3;
		DecimalFormat df=new DecimalFormat("#.##");
		String num=df.format(oilrate);
		if(oilrate==0){
			   num="0.00";
			}
		if(oilrate==Maxoilrate){
			num=""+Maxoilrate;
		}
		String text="[Gear:"+Gear+"]throttle:("+num+")";
        count=Math.floor(count);
        for(int i=0;i<count;i++){
        	bar=bar+"|";
        }
        text=text+bar;
       Font2D font=new Font2D();
       text2d=font.createText(text, 20, 1);
       text2d.setName("text2d");
        text2d.setLocalScale(1f);
        text2d.setLocalTranslation(new Vector3f(0,0,0));
        textother="car speed:"+df.format(car.getspeed());
        textother=textother+"     total oil"+df.format(car.getoil())+"";
        text2dother=font.createText(textother, 20, 1);
        text2dother.setName("text2dother");
        text2d.setLocalTranslation(new Vector3f(0,20,0));
        this.attachChild(text2d);
        this.attachChild(text2dother);
	}
	public void updatetext(RoboTank car){
		this.car=car;
		Gear=car.getGear();
		oilrate=car.getoilrate();
		double count=oilrate*3;
		DecimalFormat df=new DecimalFormat("#.##");
		String num=df.format(oilrate);
		if(oilrate==0){
			   num="0.00";
			}
		if(oilrate==Maxoilrate){
			num=""+Maxoilrate;
		}
		String text="[Gear:"+Gear+"]throttle :("+num+")";
        count=Math.floor(count);
           bar="";
        for(int i=0;i<count;i++){
        	bar=bar+"|";
        }
        if(car.getoil()<0.2){
        	bar="out of gas";
        	text2d.setLocalScale(1.7f);
            text="[Gear:"+Gear+"]throttle:(0) ";
        }
        text=text+bar;

        textother="car speed:"+df.format(car.getspeed());
        textother=textother+"          total oil:"+df.format(car.getoil())+"";
        text2dother.setText(textother);
        text2d.setText(text);
        text2d.setRandomColors();

	}

}
